﻿using System;
using System.Windows;
using System.Windows.Interactivity;

namespace EMan.GUI.Common.Behaviours
{
  public class ShowModalEntityEditorBehaviour : Behavior<BaseViewUserControl>
  {
    public static readonly DependencyProperty ViewTypeProperty =
      DependencyProperty.Register( "ViewType", typeof( Type ), typeof( ShowModalEntityEditorBehaviour ), new PropertyMetadata( default( Type ) ) );

    public static readonly DependencyProperty EditSessionProperty =
      DependencyProperty.RegisterAttached( "EditSession", typeof(object), typeof( ShowModalEntityEditorBehaviour ), new PropertyMetadata( default( DependencyObject ), OnEditSessionChanged  ) );

    public Type ViewType
    {
      get { return ( Type )GetValue( ViewTypeProperty ); }
      set { SetValue( ViewTypeProperty, value ); }
    }

    public object EditSession
    {
      get { return GetValue( EditSessionProperty ); }
      set { SetValue( EditSessionProperty, value ); }
    }

    private void OnEditSessionChanged()
    {
      if( EditSession != null )
        AssociatedObject.ShowEditor( ViewType, EditSession );
    }

    private static void OnEditSessionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      var behaviour = ( ShowModalEntityEditorBehaviour )d;
      behaviour.OnEditSessionChanged();
    }
  }
}